package team2.game.crow;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;


/**
 * Written by Tyler Reid of Studio Reveries.
 * 
 * All work provided can be modified and used freely so long as this statement stays in the source code.
 * 
 * 
 * @author Tyler Reid @ www.studioreveries.com
 *
 */
public abstract class LoadingGameActivity extends BaseGameActivity {
	
	// ===========================================================
    // Constants
    // ===========================================================

	public static final int CAMERA_WIDTH = 480;
    public static final int CAMERA_HEIGHT = 720;

	// ===========================================================
    // Fields
    // ===========================================================
    
    protected Engine engine;
    
    private TiledTextureRegion loadingWheel;
    private BitmapTextureAtlas spriteBox;
	
	// ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
	
	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	@Override
	public Engine onLoadEngine() {
		Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return engine = new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));
	}

	@Override
	public void onLoadResources() {
		
		this.spriteBox = new BitmapTextureAtlas(512,512,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.loadingWheel = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "Munnin.png",0,0,4,4);
		
		engine.getTextureManager().loadTexture(spriteBox);

	}

	@Override
	public Scene onLoadScene() {
		Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0,0,0));
		
		AnimatedSprite loadingSprite = new AnimatedSprite(0,0,loadingWheel);
		loadingSprite.animate(1000);
		loadingSprite.setPosition(CAMERA_WIDTH/2-(loadingSprite.getWidth()/2), CAMERA_HEIGHT/2-(loadingSprite.getHeight()/2));
		scene.attachChild(loadingSprite);
		
		IAsyncCallback callback = new IAsyncCallback() {
			 
			@Override
			public void workToDo() {
				assetsToLoad();
			}
 
			@Override
			public void onComplete() {
				unloadLoadingScene();
				engine.setScene(onAssetsLoaded());
			}
		};
		
		new AsyncTaskLoader().execute(callback);
		
		return scene;
	}
	
	// ===========================================================
    // Methods
    // ===========================================================
	
	private void unloadLoadingScene(){
		 
	}
 
	/**
	 * This will be called after all of the asyc assets are loaded.
	 * The loader will be in charge of changing the scenes.
	 */
	protected abstract Scene onAssetsLoaded();
 
 
	/**
	 * This is called when assets need to be loaded in the background.
	 */
	protected abstract void assetsToLoad();

}
	

    // ===========================================================
    // Getter & Setter
    // ===========================================================
